package cn.study.vue_project_xxx.manager;


import cn.study.vue_project_xxx.common.constants.BaseConstants;
import cn.study.vue_project_xxx.common.enumeration.MapKey;
import cn.study.vue_project_xxx.entity.dto.TAdminRoleDTO;
import cn.study.vue_project_xxx.entity.po.TAdminRole;
import cn.study.vue_project_xxx.entity.po.TPower;
import cn.study.vue_project_xxx.entity.po.TRole;
import cn.study.vue_project_xxx.entity.po.TRolePower;
import cn.study.vue_project_xxx.mapper.TAdminRoleMapper;
import cn.study.vue_project_xxx.mapper.TPowerMapper;
import cn.study.vue_project_xxx.mapper.TRoleMapper;
import cn.study.vue_project_xxx.mapper.TRolePowerMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.management.relation.Role;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import static cn.study.vue_project_xxx.common.enumeration.MapKey.*;

@Component
public class TadminRoleManager {

    @Resource
    TAdminRoleMapper adminRoleMapper;

    @Resource
    TRoleMapper roleMapper;

    @Resource
    TRolePowerMapper rolePowerMapper;

    @Resource
    TPowerMapper powerMapper;

    /*
    * 增加管理员角色
    * */
    @Transactional
    public Boolean addAdminRole(TAdminRole adminRole) {
        //数据准备
        TRole tRole = new TRole();
        BeanUtils.copyProperties(adminRole,tRole);
        HashMap<String, Integer> map = new HashMap<>();
        //插入数据
        int insert1 = adminRoleMapper.insert(adminRole);
        int insert2 = roleMapper.insert(tRole);
        //添加进map
        map.put(adminRoleKey,insert1);
        map.put(roleKey,insert2);

        return Objects.nonNull(map.get(adminRoleKey)) && Objects.nonNull(map.get(roleKey))
                && BaseConstants.BaseNumberTwo.equals(map.get(adminRoleKey)+map.get(roleKey));
    }

    /*
    * 修改管理员角色
    * */
    @Transactional
    public Boolean updateAdminRole(TAdminRole adminRole) {
        //数据准备
        HashMap<String, Integer> map = new HashMap<>();
        LambdaUpdateWrapper<TRole> updateWrapper = new LambdaUpdateWrapper<>();
        TRole tRole = new TRole();
        BeanUtils.copyProperties(adminRole,tRole);
        //修改数据
        int i1 = adminRoleMapper.updateById(adminRole);
        updateWrapper.eq(TRole::getRoleId,adminRole.getRoleId());
        int i2 = roleMapper.update(tRole, updateWrapper);
        //添加进map
        map.put(adminRoleKey,i1);
        map.put(roleKey,i2);
        return Objects.nonNull(map.get(adminRoleKey)) && Objects.nonNull(map.get(roleKey))
                && BaseConstants.BaseNumberTwo.equals(map.get(adminRoleKey)+map.get(roleKey));
    }

    /*
    * 删除管理员角色
    * */
    @Transactional
    public Boolean deleteAdminRole(List<Integer> roleId) {
        //数据准备
        LambdaQueryWrapper<TAdminRole> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<TRole> queryWrapper = new LambdaQueryWrapper<>();
        HashMap<String, Integer> map = new HashMap<>();
        //删除数据
        wrapper.in(TAdminRole::getRoleId,roleId);
        int i1 = adminRoleMapper.delete(wrapper);
        queryWrapper.in(TRole::getRoleId,roleId);
        int i2 = roleMapper.delete(queryWrapper);
        //添加进map
        map.put(adminRoleKey,i1);
        map.put(roleKey,i2);
        return Objects.nonNull(map.get(adminRoleKey)) && Objects.nonNull(map.get(roleKey))
                && BaseConstants.BaseNumberTwo.equals(map.get(adminRoleKey)+map.get(roleKey));
    }

    /*
    * 查询管理员角色
    * */
    public Page<TAdminRole> selectAdminRole(Integer adminId, Page<TAdminRole> pageParam) {
        //数据准备
        LambdaQueryWrapper<TAdminRole> wrapper = new LambdaQueryWrapper<>();
        //执行查询
        wrapper.eq(TAdminRole::getAdminId,adminId);
        return adminRoleMapper.selectPage(pageParam,wrapper);
    }
}
